package edu.dayu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.dayu.mapper.BlogMapper;
import edu.dayu.mapper.TagMapper;
import edu.dayu.mapper.TypeMapper;
import edu.dayu.pojo.Blog;
import edu.dayu.pojo.Tag;
import edu.dayu.pojo.Type;
import edu.dayu.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private TagMapper tagMapper;

    @Override
    public PageInfo<Blog> listBlog(Blog blog) {
        if (blog.getPageNum()<1){
            blog.setPageNum(1);
        }
        PageHelper.startPage(blog.getPageNum(), blog.getPageSize());
        List<Blog> blogs = new ArrayList<>();
        if (blog.getTypeid()!=0) {
            List<Blog> listTypeBlog = blogMapper.listTypeBlog(blog);
            List<Blog> blog1 = new ArrayList<>();
            for (Blog blog2 : listTypeBlog) {
                blog2.setTitle(blog.getTitle());
                blog1.add(blog2);
            }
            for (Blog blog2 : blog1) {
                Blog blog3 = blogMapper.getBlog(blog2);
                if (blog3 != null)
                    blogs.add(blog3);
            }
        }else if(blog.getTagid()!=0){
            int[] ints = blogMapper.listTagBlogId(blog.getTagid());
            Tag tag = new Tag();
            tag.setId(blog.getTagid());
            Tag tagName = tagMapper.getTagName(tag);
            for (int i : ints) {
                blog.setId(i);
                Blog blog1 = blogMapper.getBlog(blog);
                Type type = typeMapper.getType(blogMapper.getBlogTypeId(i));
                blog1.setTag(tagName.getName());
                blog1.setType(type.getName());
                blogs.add(blog1);
            }
        }else{
             blogs = blogMapper.listBlog(blog);
        }
        //将分类的名字加入每个blog对象
        List<Blog> blogList = new ArrayList<>();
        for (Blog blog1 : blogs) {
            Type type = typeMapper.getType(blogMapper.getBlogTypeId(blog1.getId()));
            blog1.setType(type.getName());
            blogList.add(blog1);
        }
        PageInfo<Blog> pageInfo = new PageInfo<>(blogList);
        Map<String ,Object> map = new HashMap<>();
        int i =1;
        if (blogList.size()>0) {
            //查询得到的结果个数
            if (blog.getTypeid() != 0) {
                List<Integer> list = typeMapper.listType_Blogs(blog.getTypeid());
                map.put("bloglist", list);
            }
            if (blog.getTitle() != null && blog.getTitle() != "")
                map.put("title", blog.getTitle());
            if (blog.getTagid()!=0){
                List<Integer> list = typeMapper.listTag_Blogs(blog.getTagid());
                map.put("bloglist",list);
            }
            i = blogMapper.countBlogType(map);
        }
        pageInfo.setPageNum(blog.getPageNum());
        pageInfo.setPageSize(blog.getPageSize());
        pageInfo.setPages(i%pageInfo.getPageSize()==0?i/pageInfo.getPageSize():i/ pageInfo.getPageSize()+1);
        return pageInfo;
    }

    @Override
    public PageInfo<Blog> listBlogTag(Blog blog) {
        PageHelper.startPage(blog.getPageNum(),blog.getPageSize());
        List<Blog> blogList = new ArrayList<>();
        if (blog.getTagid()!=0) {
            int[] ints = blogMapper.listTagBlogId(blog.getTagid());
            Tag tag = new Tag();
            tag.setId(blog.getTagid());
            Tag tagName = tagMapper.getTagName(tag);
            for (int i : ints) {
                blog.setId(i);
                Blog blog1 = blogMapper.getBlog(blog);
                Type type = typeMapper.getType(blogMapper.getBlogTypeId(i));
                blog1.setTag(tagName.getName());
                blog1.setType(type.getName());
                blogList.add(blog1);
            }
        }else{
            List<Blog> blogs = blogMapper.listBlog(blog);
            for (Blog blog1 : blogs) {
                Type type = typeMapper.getType(blogMapper.getBlogTypeId(blog1.getId()));
                blog1.setType(type.getName());
                blogList.add(blog1);
            }
        }
        PageInfo<Blog> blogPageInfo = new PageInfo<>(blogList);
        return blogPageInfo;
    }

    @Override
    public Blog getBlog(Blog blog) {
        Blog blog1 = blogMapper.getBlog(blog);
        int blogTypeId = blogMapper.getBlogTypeId(blog.getId());
        Type type = typeMapper.getType(blogTypeId);
        blog1.setTypeid(blogTypeId);
        blog1.setType(type.getName());
        int [] list = blogMapper.listTagBlog(blog.getId());
        if (list.length>0) {
            StringBuffer tags = new StringBuffer();
            for (int i =0; i < list.length; i++) {
                tags.append(list[i]);
                if (i<list.length-1)
                tags.append(",");
            }
            blog1.setTags(new String(tags));
        }
        return blog1;
    }

    @Override
    public Boolean saveBlog(Blog blog) {
        blog.setCreateTime(new Date());
        blog.setUpdateTime(new Date());
        Boolean aBoolean = blogMapper.saveBlog(blog);
        int blogsId = blogMapper.getBlogsId(blog);
        blog.setId(blogsId);
        blogMapper.saveTypeBlogs(blog);
        if (blog.getTagList()!=null) {
            for (int i : blog.getTagList()) {
                blog.setTagid(i);
                blogMapper.saveTagBlogs(blog);
            }
        }
        return aBoolean;
    }

    @Override
    public Boolean updateBlog(Blog blog) {
        blog.setUpdateTime(new Date());
        Boolean aBoolean = blogMapper.updateBlog(blog);
        blogMapper.updateTypeBlogs(blog);
        if (blog.getTagList()!=null) {
            tagMapper.deleteTagBlog(blog.getId());
            for (int i : blog.getTagList()) {
                blog.setTagid(i);
                blogMapper.saveTagBlogs(blog);
            }
        }
        return aBoolean;
    }

    @Override
    public void deleteBlog(int id) {
        blogMapper.deleteBlog(id);
        typeMapper.deleteTypeBlog(id);
        int [] list = blogMapper.listTagBlog(id);
        if (list.length>0)
        tagMapper.deleteTagBlog(id);
    }

    @Override
    public int countBlog() {
        Map<String ,Object> map = new HashMap<>();
        map.put("published",2);
        return blogMapper.countBlogType(map);
    }

    @Override
    public void updateBlogViews(Blog blog) {
        blogMapper.updateBlogViews(blog);
    }

    @Override
    public Map<String,List<Blog>> listBlogByYear() {
        List<String> strings = blogMapper.listYear();
        Map<String,List<Blog>> map = new HashMap();
        for (String string : strings) {
            List<Blog> blogList = blogMapper.listBlogByYear(string);
            map.put(string,blogList);
        }
        return map;
    }


}
